package com.ruoyi.quartz.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.mapper.CornLogMapper;
import com.ruoyi.quartz.mapper.CustomerMapper;
import com.ruoyi.quartz.mapper.OrderMapper;
import com.ruoyi.quartz.service.IOrderService;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.quartz.util.ScheduleUtils;
import com.ruoyi.system.domain.DTO.*;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.IRyAdminService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<SocVo> selectList(SocDto socDto) throws ParseException {
        List<SocVo> socVoList = new ArrayList<>();
        socVoList = orderMapper.selectList(socDto);
        if(socVoList != null && !socVoList.isEmpty() && socVoList.size() > 0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SocVo socVo : socVoList) {
                //图片地址拼接
                socVo.setZs((socVo.getImgUrl()==null?"":socVo.getImgUrl())+(socVo.getZs()==null?"":socVo.getZs()));
                socVo.setBgdImg((socVo.getImgUrl2()==null?"":socVo.getImgUrl2())+(socVo.getBgdImg()==null?"":socVo.getBgdImg()));
                //时间判断
                Date end = DateUtil.offsetDay(sdf.parse(socVo.getCreateTime()),180);
                int num = (int) DateUtil.between(new Date(),end, DateUnit.DAY);
                if(num <=30 && num > 15){
                    socVo.setCoalorFlag("1");
                }else if(num <=15 && num>7){
                    socVo.setCoalorFlag("2");
                }else if(num <=7){
                    socVo.setCoalorFlag("3");
                }
            }
        }
        return socVoList;
    }

    @Override
    public SocVo selectById(String id) {
        SocVo socVo = new SocVo();
        socVo = orderMapper.selectById(id);
        //图片地址拼接
        socVo.setZs((socVo.getImgUrl()==null?"":socVo.getImgUrl())+(socVo.getZs()==null?"":socVo.getZs()));
        socVo.setBgdImg((socVo.getImgUrl2()==null?"":socVo.getImgUrl2())+(socVo.getBgdImg()==null?"":socVo.getBgdImg()));
        return socVo;
    }

    @Override
    public Boolean deleteById(String ids) {
        if(ids.contains(",")){
            String[] strs = ids.split(",");
            for (String str : strs) {
                SocVo socVo = new SocVo();
                socVo.setId(Long.parseLong(str));
                socVo.setStatus("0");
                socVo.setUpdateBy(ShiroUtils.getLoginName());
                orderMapper.deleteById(socVo);
            }
            return true;
        }else{
            SocVo socVo = new SocVo();
            socVo.setId(Long.parseLong(ids));
            socVo.setStatus("0");
            socVo.setUpdateBy(ShiroUtils.getLoginName());
            return orderMapper.deleteById(socVo);
        }
    }

    @Override
    public Boolean cancleById(String ids) {
        if(ids.contains(",")){
            String[] strs = ids.split(",");
            for (String str : strs) {
                SocVo socVo = new SocVo();
                socVo.setId(Long.parseLong(str));
                socVo.setStatus("2");
                socVo.setUpdateBy(ShiroUtils.getLoginName());
                orderMapper.deleteById(socVo);
            }
            return true;
        }else{
            SocVo socVo = new SocVo();
            socVo.setId(Long.parseLong(ids));
            socVo.setStatus("2");
            socVo.setUpdateBy(ShiroUtils.getLoginName());
            return orderMapper.deleteById(socVo);
        }
    }

    @Override
    public Boolean copy(String ids) {
        if(ids.contains(",")){
            String[] strs = ids.split(",");
            for (String str : strs) {
                SocVo socVo = new SocVo();
                socVo = orderMapper.selectById(str);
                SocVo insertSocVo = new SocVo();
                BeanUtils.copyProperties(socVo,insertSocVo);
                Boolean result = orderMapper.saveSocVo(insertSocVo);
                if(result){
                    SocVo updateSocVo = new SocVo();
                    updateSocVo.setId(insertSocVo.getId());
                    updateSocVo.setSort(insertSocVo.getId().toString());
                    orderMapper.editSocVo(updateSocVo);
                }else{
                    return false;
                }
            }
            return true;
        }else{
            SocVo socVo = new SocVo();
            socVo = orderMapper.selectById(ids);
            SocVo insertSocVo = new SocVo();
            BeanUtils.copyProperties(socVo,insertSocVo);
            Boolean result = orderMapper.saveSocVo(insertSocVo);
            if(result){
                SocVo updateSocVo = new SocVo();
                updateSocVo.setId(insertSocVo.getId());
                updateSocVo.setSort(insertSocVo.getId().toString());
                return orderMapper.editSocVo(updateSocVo);
            }else{
                return false;
            }
        }
    }

    @Override
    public AjaxResult saveSocVo(SocVo socVo) {
        Boolean result = orderMapper.saveSocVo(socVo);
        if(result){
            SocVo updateSocVo = new SocVo();
            updateSocVo.setId(socVo.getId());
            updateSocVo.setSort(socVo.getId().toString());
            orderMapper.editSocVo(updateSocVo);
            return AjaxResult.success("成功");
        }else{
            return AjaxResult.error("添加失败");
        }
    }
    @Override
    public AjaxResult editSocVo(SocVo socVo) {
        Boolean result = orderMapper.editSocVo(socVo);
        if(result){
            return AjaxResult.success("成功");
        }else{
            return AjaxResult.error("添加失败");
        }
    }

}
